package top.zhangjianyong.tools.validation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author zhangjianyong
 * @since 2025/4/3 上午10:30
 */
public class NumberValueValidator implements ConstraintValidator<NumberValue, Integer> {
    private int[] allowedValues;

    @Override
    public void initialize(NumberValue constraintAnnotation) {
        this.allowedValues = constraintAnnotation.allowedValues();
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        if (value == null) return false;
        for (int allowedValue : allowedValues) {
            if (value == allowedValue) {
                return true;
            }
        }
        return false;
    }
}
